// License Notification
//======================================================
// Copyright (c) 2010 Channel Intelligence Inc.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// ======================================================




using System;
using System.Reflection;

namespace CIP4.Common
{

    public static class CimrExtensionMethods
    {

        /// <summary>
        /// If the string is null or empty, Seperator is empty, otherwise Seperator returns the seperator value
        /// </summary>
        /// <param name="fromValue"></param>
        /// <param name="seperator"></param>
        /// <returns></returns>
        public static string Seperator(this string fromValue, string seperator)
        {
            if (string.IsNullOrEmpty(fromValue))
                return string.Empty;
            return seperator;
        }

        /// <summary>
        /// If the string is null or empty, Seperator returns the firstSeperator value, otherwise it returns the subsequentSeperator value
        /// </summary>
        /// <param name="fromValue"></param>
        /// <param name="firstSeperator"></param>
        /// <param name="subsequentSeperator"></param>
        /// <returns></returns>
        public static string Seperator(this string fromValue, string firstSeperator, string subsequentSeperator)
        {
            if (string.IsNullOrEmpty(fromValue))
                return firstSeperator;
            return subsequentSeperator;
        }

        public static int StringToEnum(this string fromValue, Type enumType)
        {
            object avoidsNullParamterWarning = new object();
            foreach (FieldInfo mem in enumType.GetFields())
            {
                if (mem.IsLiteral)
                {
                    if (mem.Name == fromValue)
                        return (int)mem.GetValue(avoidsNullParamterWarning);
                }
            }
            return 0;
        }
    }
}